// 引入样式
import "../../assets/css/normalize.less";
import "../../assets/css/reset.less";
import "../../assets/css/property.less";
import "../../assets/css/basic.less";
import "../../assets/fonts/font_kok358gumca/iconfont.css";
import "../../assets/fonts/font_iysuojan2xi/iconfont.css";

import "./running.less";

import { $ } from "../../utils/dom";

/**
 * 开始计时
 */

let time = 0;
setInterval(() => {
	++time;
	let h = Math.floor(time / 60 / 60);
	let m = Math.floor((time / 60) % 60);
	let s = time % 60;
	// 补零
	$("#timeBox").textContent = [h, m, s]
		.map((v) => {
			return v >= 10 ? v : `0${v}`;
		})
		.join(":");
        // 计算运动的公里
        $("#kilo").textContent =  ((time*2)/1000).toFixed(2)
        // 计算速度保留两位小数
        $("#spaceBox").textContent =  calcPace(((time*2)/1000).toFixed(2), time)
        //计算千卡
       $("#calorieBox").textContent =  (calCalorie(time * 2)/1000).toFixed(2)

}, 1000);

/**
 * 计算配速函数
 * d:距离
 * s:时间
 */
function calcPace(d, s) {
	let distance = d;
	let spendTime = s / 60;
	if (distance > 0 && spendTime > 0) {
		let pace = parseFloat(spendTime / distance);
		let m = Math.floor(pace);
		let s = ((pace - m) * 60).toFixed(0);
		return `${m}'${s}'`;
	}
	return "--";
}

/**
 * 计算卡路里
 */

function calCalorie(miles) {
	if (miles > 0) {
		let calorie = 60 * miles * 1.036;
		return calorie.toFixed(2);
	}

	return "--";
}


/**
 * 点击暂停显示继续按钮和停止按钮
 */
$("#pauseBox").addEventListener("click",()=>{
    $("#continueBox").classList.add("dblock");
    $("#stopBox").classList.add("dblock");
    $("#pauseBox").classList.add("dpnone");
})


$("#continueBox").addEventListener("click",()=>{
    $("#stopBox").classList.add("dpnone");
    $("#continueBox").classList.add("dpnone");
    $("#pauseBox").classList.add("dblock");
})